package com.jimet.objects;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Vector;

public class FilterParamsNew {
	
	private LinkedHashMap<String,Vector<String>> paramsHash;
	private Class<?> className;
	private int paramSize ;
	private String page;
	private String divId;
	private String strFilterValues;
	private String lasFilterPage;
	
	/**
	 * 
	 */
	public FilterParamsNew()
	{
		this.paramsHash 	= new LinkedHashMap<String, Vector<String>>();
		this.strFilterValues = new String();
		this.lasFilterPage	 = new String();
		
		this.paramSize	 	= 0;
		this.page = "";
		this.divId = "";
	}
	
	public String getLasFilterPage() {
		return lasFilterPage;
	}

	public void setLasFilterPage(String lasFilterPage) {
		this.lasFilterPage = lasFilterPage;
	}

	public String getStrFilterValues() {
		return strFilterValues;
	}

	public void setStrFilterValues(String strFilterValues) {
		this.strFilterValues = strFilterValues;
	}

	public void addParam( String labelName , String paramName, String paramValue , String paramText , String type , String operator,String child,String parent)
	{
		if(hasKeyLike(paramName))
		{
			paramName = paramName + "_secondary";
		}
		
		Vector<String> paramsVec = new Vector<String>();
		paramsVec.add(labelName);
		paramsVec.add(type);
		paramsVec.add(operator);
		paramsVec.add(paramValue);
		paramsVec.add(paramText);
		paramsVec.add(child);
		paramsVec.add(parent);
		
		this.paramsHash.put(paramName, paramsVec);
		this.paramSize = this.paramsHash.size();
		
	}

	public void addParam( String labelName , String paramName, String paramValue , String paramText , String type , String operator)
	{
		if(hasKeyLike(paramName))
		{
			paramName = paramName + "_secondary";
		}
			
		Vector<String> paramsVec = new Vector<String>();
		paramsVec.add(labelName);
		paramsVec.add(type);
		paramsVec.add(operator);
		paramsVec.add(paramValue);
		paramsVec.add(paramText);
		paramsVec.add(null);
		paramsVec.add(null);
		
		this.paramsHash.put(paramName, paramsVec);
		this.paramSize = this.paramsHash.size();
		
	}
	
	public Vector<String> getParamValue( String paramName )
	{
		return this.paramsHash.get(paramName);
	}
	
	public String getParamStrValue( String paramName )
	{
		return (String) this.paramsHash.get(paramName).get(3);
	}
	
	public void overrideParamValue( String paramName , String newValue )
	{
		Vector<String> paramsVec =  this.getParamValue(paramName);
		paramsVec.set(3, newValue);
	}
	
	public void overrideParamOperator( String paramName , String operator )
	{
		Vector<String> paramsVec =  this.getParamValue(paramName);
		paramsVec.set(2, operator);
	}
	
	public void overrideParamText( String paramName , String text )
	{
		Vector<String> paramsVec =  this.getParamValue(paramName);
		paramsVec.set(4, text);
	}
	
	
	public LinkedHashMap<String,Vector<String>> getFilterParams()
	{
		return this.paramsHash;
	}
	
	public void removeParam( String paramName )
	{
		this.paramsHash.remove(paramName);
		this.paramSize = this.paramsHash.size();
	}
	
	public int getParamsSize()
	{
		return this.paramSize;
	}

	public String getPage() {
		return page;
	}
	
	
	public String getDivId() {
		return divId;
	}

	public void setPage(String page, String divId) {
		this.page = page;
		this.divId = divId;
		this.paramsHash 		= new LinkedHashMap<String, Vector<String>>();
	}
	
	
	public HashMap<String, String> setFilterVec(HashMap<String, String> pagingVec) throws Exception
	{
		LinkedHashMap<String,Vector<String>> parametersHash	= null;
		
		SimpleDateFormat dFormat2V 			= new SimpleDateFormat("dd.MM.yyyy HH:mm",new Locale("tr","TR"));
		SimpleDateFormat dFormat2VSecondary = new SimpleDateFormat("yyyy-MM-dd HH:mm:00.0",new Locale("tr","TR"));
		SimpleDateFormat dFormatV 			= new SimpleDateFormat("dd.MM.yyyy",new Locale("tr","TR"));

		strFilterValues	= "";
		int paramSize = this.getParamsSize();
		if( paramSize > 0 )
		{
			parametersHash  = this.getFilterParams();
			Iterator<String> iter = parametersHash.keySet().iterator(); 
			while ( iter.hasNext() )
			{

				String paramKey = (String) iter.next();
					
				Vector<String> paramVec = (Vector<String>) parametersHash.get(paramKey);
				String type 			= paramVec.get(1);
				String op 				= paramVec.get(2);
				String val  			= paramVec.get(3);
				String txt				= paramVec.get(4);
				String lbl 				= paramVec.get(0);
				//String child 			= paramVec.get(5);
				//String parent 		= paramVec.get(6);
				if(val.equals("-1") || val.equals(""))
					continue;
				
				if(type.equals("combo"))
				{
					strFilterValues	+= "<span style=\"\"><font color=\"#444444\">"+ lbl+ "</font>  \"" + txt + "\" </span> , ";
				}
				else if(type.equals("text"))
				{
					strFilterValues	+= "<span style=\"\"><font color=\"#444444\">"+lbl+ "</font>  \"" + val + "\" </span>, ";
					val = " '%"+val+"%' ";
				}
				else if(type.equals("datetime"))
				{
					strFilterValues	+= "<span style=\"\"><font color=\"#444444\">"+ lbl+ "</font>  \"" + 
						dFormat2V.format(new Timestamp( dFormat2V.parse(val).getTime() )) + 
						"\" </span> , ";					
					val = " '"+new Timestamp( dFormat2V.parse(val).getTime() ) +"' ";	
				}
				else if(type.equals("date"))
				{
					strFilterValues	+= "<span style=\"\"><font color=\"#444444\">"+ lbl+ "</font>  \"" + 
						dFormatV.format(new Timestamp( dFormatV.parse(val).getTime() )) 
						+ "\" </span> , ";
					val = " '"+new Timestamp( dFormatV.parse(val).getTime() ) +"' ";
				}
				else
					strFilterValues	+= "<span style=\"\"><font color=\"#444444\">"+ lbl+ "</font>  \"" + val + "\" </span> , ";
				
				if(paramKey.endsWith("_secondary"))
				{	
					if(type.equals("date"))
					{
						val = val.replaceAll("'","");
						Timestamp t = new Timestamp( dFormat2VSecondary.parse(val).getTime() );
						Calendar cal = Calendar.getInstance();
						cal.setTimeInMillis(t.getTime());
						cal.set(Calendar.HOUR_OF_DAY,23);
						cal.set(Calendar.MINUTE,59);
						cal.set(Calendar.SECOND,59);
						//cal.set(Calendar.MILLISECOND,999999);
						val = "'"+new Timestamp( cal.getTimeInMillis()  )+"'";
					}
					
					paramKey = paramKey.replaceAll("_secondary","");
					pagingVec.put(paramKey, (((String) pagingVec.get(paramKey))!=null?((String) pagingVec.get(paramKey))+"#T#":"") + " "+op+val);
				}
				else
				{
					pagingVec.put(paramKey," "+op+val);
				}
			}
			
			if ( strFilterValues.length() > 0 )
				strFilterValues	= strFilterValues.substring(0,strFilterValues.length()-2);

			this.setStrFilterValues(strFilterValues);
		}
			
		return pagingVec;
	}
	
	public boolean hasKeyLike( String searchKey )
	{
		if( this.paramsHash == null )
			return false;
		
		return this.paramsHash.containsKey(searchKey);
	}

	public void setClassName(Class<?> className) {
		this.className = className;
	}

	public Class<?> getClassName() {
		return className;
	}

	
}
